package com.sjd.storehouse.controller;

import com.sjd.storehouse.entity.User;
import com.sjd.storehouse.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/userController")
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping("/login")
    @ResponseBody
    ModelAndView login(User user) {
        ModelAndView mv = new ModelAndView();
        //登录名密码空
        if (user.getAccount().isEmpty() || user.getPwd().isEmpty()) {
            mv.addObject("errMsg", "登录名或密码不能为空");
            mv.setViewName("/login");
            return mv;
        }
        String msg = userService.login(user.getAccount(), user.getPwd());
        if (msg.isEmpty()) {
            mv.setViewName("redirect:/index?account="+user.getAccount());
        } else {
            mv.addObject("errMsg", msg);
            mv.setViewName("/login");
        }
        return mv;
    }



    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/";
    }

}
